package com.zwz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zwz.bean.AdminInfo;
import com.zwz.dao.AdminInfoDao;
import com.zwz.service.AdminInfoService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AdminInfoServiceImpl implements AdminInfoService {

    @Autowired
    private AdminInfoDao adminInfoDao;

    @Override
    public boolean insert(AdminInfo adminInfo) {
        int result = adminInfoDao.insert(adminInfo);
        if (result == 0){
            return false;
        }else {
            return true;
        }
    }

    @Override
    public boolean deleteById(Integer aid) {
        int result = adminInfoDao.deleteById(aid);

        if (result == 0){
            return false;
        }else {
            return true;
        }
    }

    @Override
    public boolean updateById(AdminInfo adminInfo) {
        int result = adminInfoDao.updateById(adminInfo);
        if (result == 0){
            return false;
        }else {
            return true;
        }
    }

    @Override
    public AdminInfo getById(Integer aid) {
        AdminInfo adminInfo = adminInfoDao.selectById(aid);
        return adminInfo;
    }

    @Override
    public IPage<AdminInfo> getPage(int currentPage, int pageSize, AdminInfo adminInfo) {
        IPage<AdminInfo> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<AdminInfo> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(adminInfo.getUserName()), AdminInfo::getUserName, adminInfo.getUserName());
        lqw.like(Strings.isNotEmpty(adminInfo.getPhone()), AdminInfo::getPhone, adminInfo.getPhone());
        lqw.orderByDesc(AdminInfo::getAid);
        adminInfoDao.selectPage(page, lqw);
        return page;
    }

    @Override
    public int countAll() {
        int result = adminInfoDao.countAll();
        return result;
    }

    @Override
    public boolean adminUpdateInfo(AdminInfo adminInfo) {
        UpdateWrapper<AdminInfo> userInfoUpdateWrapper = new UpdateWrapper<>();
        userInfoUpdateWrapper.eq("id",adminInfo.getId());
        return adminInfoDao.update(adminInfo,userInfoUpdateWrapper) == 1;
    }

    @Override
    public AdminInfo adminShow(Integer id) {
        QueryWrapper<AdminInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return adminInfoDao.selectOne(queryWrapper);
    }
}
